package com.example.demo.mapper;

import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2024-01-24
 * Time: 10:42
 */
@Mapper
public interface UserInfoMapper {
//    @Select("select * from test")
//    List<UserInfo> selectAll();

    @Select("select * from test where id=3")
    UserInfo selectOne();
//
////    动态选择Id
    @Select("select * from test where id=${id}")
    UserInfo selectOneOne(Integer id);

    @Select("select * from test3 where name='${name}'")
    List<UserInfo> selectNmae(String name);
//
////    参数重命名
//    @Select("select * from test where id=#{userId}")
//    UserInfo selectOneOneOne(@Param("userId") Integer id);

//    insert
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into test2(name,age) values(#{name},#{age})") //方法实现
    Integer insert(UserInfo userInfo); //方法声明

    //参数为对象的重命名
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into test2(name,age) values(#{USERINFO.name},#{USERINFO.age})") //方法实现
    Integer insert1(@Param("USERINFO") UserInfo userInfo); //方法声明

    //delete
    @Delete("delete from test2 where id=#{id}")
    Integer delete(Integer id);

    //update
    @Update("update test2 set age=#{age} where id=#{id}")
    Integer update(UserInfo userInfo);

    //危险操作
//    @Select("select * from test3")
//    List<UserInfo> selectAll();

    @Select("select id ,name ,age ,gender," +
            " create_time as createTime,update_time as updateTime" +
            " from test3")
    List<UserInfo> selectAll();

    @Results(id = "changeMpe",value = {
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select id ,name ,age ,gender," +
            " create_time ,update_time " +
            " from test3")
    List<UserInfo> selectAll1();

    @ResultMap(value = "changeMpe")
    @Select("select id ,name ,age ,gender," +
            " create_time ,update_time " +
            " from test3")
    List<UserInfo> selectAll2();

    @Select("select id ,name ,age ,gender," +
            " create_time ,update_time " +
            " from test3")
    List<UserInfo> selectAll3();


    //排序操作
    @Select("select * from test3 order by id ${sort}")
    List<UserInfo> selectBySort(String sort);

    //模糊查找
    @Select("select * from test3 where name like concat(''%',${name},'%'')")
    List<UserInfo> selectByLike(String name);

}
